from flask import Flask, request, jsonify
from flask_cors import CORS

app = Flask(__name__)
CORS(app)  # 允许跨域请求


# 开发者数据，新增了 'nationCode' 字段
developers = [
    {"id": 1, "name": "Alice", "talentRank": 95, "nation": "China", "nationCode": "cn", "field": "Web Development",
     "name_id": "alice123", "email": "alice@example.com", "projects": ["Project A", "Project B"],
     "contributions": {"Project A": 150, "Project B": 200}, "repo_publish_date": "2023-01-10",
     "tech_stack": ["HTML", "CSS", "JavaScript"], "repo_description": "A web development project.",
     "project_size": "Large"},
    {"id": 2, "name": "Bob", "talentRank": 90, "nation": "USA", "nationCode": "us", "field": "Data Science",
     "name_id": "bob456", "email": "bob@example.com", "projects": ["Project C"],
     "contributions": {"Project C": 50}, "repo_publish_date": "2022-11-20",
     "tech_stack": ["Python", "Pandas", "NumPy"], "repo_description": "Data analysis project.",
     "project_size": "Medium"},
    {"id": 3, "name": "Charlie", "talentRank": 85, "nation": "UK", "nationCode": "gb", "field": "Mobile Development",
     "name_id": "charlie789", "email": "charlie@example.com", "projects": ["Project D", "Project E"],
     "contributions": {"Project D": 120, "Project E": 180}, "repo_publish_date": "2023-05-15",
     "tech_stack": ["Swift", "Objective-C"], "repo_description": "Mobile app project.", "project_size": "Small"},
    {"id": 4, "name": "Aharlie", "talentRank": 100, "nation": "UK", "nationCode": "gb", "field": "Mobile Development",
     "name_id": "aharlie101", "email": "aharlie@example.com", "projects": ["Project F"],
     "contributions": {"Project F": 300}, "repo_publish_date": "2023-07-01",
     "tech_stack": ["Java", "Kotlin"], "repo_description": "Advanced mobile development project.",
     "project_size": "Large"}
]

@app.route('/api/data', methods=['GET'])
def get_developers():
    # 获取查询参数
    query = request.args.get('query', '')
    search_type = request.args.get('type', 'name')  # 获取搜索类型，默认为'name'

    # 根据查询参数过滤开发者
    filtered_developers = []
    for dev in developers:
        if search_type == 'name' and query.lower() in dev['name'].lower():
            filtered_developers.append(dev)
        elif search_type == 'nation' and query.lower() in dev['nation'].lower():
            filtered_developers.append(dev)
        elif search_type == 'field' and query.lower() in dev['field'].lower():
            filtered_developers.append(dev)
        elif search_type == 'email' and query.lower() in dev['email'].lower():
            filtered_developers.append(dev)

    return jsonify({"developers": filtered_developers})





@app.post('/api/submit')
def submit():
    data = request.get_json()  # 获取 JSON 数据
    name = data.get('name')
    email = data.get('email')

    print(f'收到数据: 姓名 - {name}, 邮箱 - {email}')

    return jsonify({'message': '数据接收成功'})

# # 处理 GET 请求的路由
# @app.get('/api/data')
# def get_data():
#     # 假设返回一些静态数据
#     sample_data = {
#         'searchQuery': '1',
#         'developers': ['a']
#     }
#     return jsonify(sample_data)

if __name__ == '__main__':
    app.run(debug=True, port=5000)